import numpy as np
import torch
from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt


def f(x):
    return x * torch.cos(np.pi * x)


def g(x):
    return f(x) + 0.2 * torch.cos(5 * np.pi * x)


x = torch.arange(0.5, 1.5, 0.02)
fig = plt.figure(figsize=(13.5, 2.5))
plt.rcParams['font.sans-serif'] = 'Microsoft YaHei'

ax1 = fig.add_subplot(131)
ax1.axis("off")
ax1.plot(x, g(x), 'b-')
ax1.set_title("局部最小值风险")

x = torch.arange(-2., 2., 0.02)
ax2 = fig.add_subplot(132)
ax2.axis("off")
ax2.plot(x, x ** 3, 'g-')
ax2.set_title("鞍点")

x = torch.arange(-2., 5., 0.02)
ax3 = fig.add_subplot(133)
ax3.axis("off")
ax3.plot(x, (1 - torch.exp(-x)) / (1 + torch.exp(-x)), 'g-')
ax3.set_title("梯度消失")

plt.show()
